#include "thread.h"
#include <QDebug>
Thread::Thread(QObject *parent) : QObject(parent)
{
     m_SerialPort=new QSerialPort();
     m_Timer=new QTimer;
     m_Thread=new QThread(this);

     this->moveToThread(m_Thread);//this移动到线程
     m_SerialPort->moveToThread(m_Thread);//串口移动到线程
     m_Timer->moveToThread(m_Thread);//定时器移动到线程

     m_Thread->start();//启动线程

     connect(this,&Thread::sig_runTimer,this,&Thread::slots_runTimer);
     connect(m_Timer,&QTimer::timeout,this,&Thread::slots_timeOut);
     connect(this,&Thread::sig_wirteModbus,this,&Thread::slots_wirteModbus);

     //串口波特率
     m_SerialPort->setBaudRate(QSerialPort::Baud115200);
     //串口校验位
     m_SerialPort->setParity(QSerialPort::NoParity);
     //串口数据位
     m_SerialPort->setDataBits(QSerialPort::Data8);
     //串口停止位
     m_SerialPort->setStopBits(QSerialPort::OneStop);
     //串口流控制
     m_SerialPort->setFlowControl(QSerialPort::NoFlowControl);
    //串口名称
     m_SerialPort->setPortName("COM6");
     if(!m_SerialPort->open(QIODevice::ReadWrite)){

         qDebug()<<QString("打开串口 %0 失败").arg(m_SerialPort->portName());
         return;
     }else{
         qDebug()<<QString("打开串口 %0 成功，启动定时器").arg(m_SerialPort->portName());
          emit sig_runTimer();
     }

    //寄存器设置
     data_float.type=_REGISTER_TYPE::_FLOAT32;
     data_float._Regisetr_U=0x00;//读取0x00开始的32位浮点型数据
     data_float._Regisetr_L=0x00;//读取0x00开始的32位浮点型数据

     data_int.type=_REGISTER_TYPE::_INT32;
     data_int._Regisetr_U=0x00;//读取0x02开始的32位有符号整形数据
     data_int._Regisetr_L=0x02;//读取0x02开始的32位有符号整形数据

     master_1=ModbusMaster(_MODBUS_TYPE::_MODBUSRTU,_FUNCTION_CODE::_CODE_03);//生成一个modbusRTU协议的03功能码的主站
     master_1.setModbusAddr(0x01);//设置设备地址
     //添加需要读取的寄存器
     master_1.appendData(data_float);
     master_1.appendData(data_int);


     data_bool.type=_REGISTER_TYPE::_BOOL;
     data_bool._Regisetr_U=0x00;//读取0x00开始的开关型数据
     data_bool._Regisetr_L=0x00;//读取0x00开始的开关型数据
     master_2=ModbusMaster(_MODBUS_TYPE::_MODBUSRTU,_FUNCTION_CODE::_CODE_01);//生成一个modbusRTU协议的01功能码的主站
     master_2.setModbusAddr(0x01);//设置设备地址
     master_2.appendData(data_bool);//添加需要读取的寄存器
}

Thread::~Thread()
{

    m_SerialPort->deleteLater();
    m_Timer->deleteLater();
    m_Thread->quit();
    m_Thread->wait();
    qDebug()<<"~Thread()";

}

void Thread::slots_runTimer()
{
    //定时读取设备
    m_Timer->start(1000);
}

void Thread::slots_timeOut()
{
    if(!m_SerialPort->isOpen()){
        qDebug()<<"串口未打开";
        return;
    }
    Recdata Updata;
    //读取数值型数据====================================================
    QByteArray sendByte_03=master_1.SendDataToByte();//生成报文
   // qDebug()<<"发送的03功能码报文"<<sendByte_03.toHex().toUpper();

    m_SerialPort->write(sendByte_03);
    m_SerialPort->waitForBytesWritten(30);
    QThread::msleep(100);
    QByteArray RecData = m_SerialPort->readAll();
    while (m_SerialPort->waitForReadyRead(10))
        RecData += m_SerialPort->readAll();
    if(!RecData.isEmpty()&&master_1.RespondByteIsValid(RecData,_FUNCTION_CODE::_CODE_03)==0)
    {
        if(master_1.RespondByteRead(RecData)!=0){
            //qDebug()<<"返回数据错误";
        }
        QVector<_MODBUS_DATA> data_1=master_1.data();
        //qDebug()<<"返回的数据是->> "<<data_1[0].m_float<<data_1[1].m_int32<<RecData.toHex().toLower();
        Updata.data_float=data_1[0].m_float;
        Updata.data_int=data_1[1].m_int32;
    }else{
        qDebug()<<"返回错误 03";
    }

    QThread::msleep(100);
    //读取开关型型数据====================================================
    QByteArray sendByte_01=master_2.SendDataToByte();//生成报文
    //qDebug()<<"发送的01功能码报文"<<sendByte_01.toHex().toUpper();

    m_SerialPort->write(sendByte_01);
    m_SerialPort->waitForBytesWritten(30);
    QThread::msleep(100);
    QByteArray RecData_01 = m_SerialPort->readAll();
    while (m_SerialPort->waitForReadyRead(10))
        RecData_01 += m_SerialPort->readAll();
    if(!RecData_01.isEmpty()&&master_2.RespondByteIsValid(RecData_01,_FUNCTION_CODE::_CODE_01)==0)
    {
        if(master_2.RespondByteRead(RecData_01)!=0){
            //qDebug()<<"返回数据错误";
        }
        QVector<_MODBUS_DATA> data_2=master_2.data();
        //qDebug()<<"返回的数据是->> "<<data_2[0].m_bool<<RecData_01.toHex().toLower();
        Updata.data_bool=data_2[0].m_bool;
    }else{
        qDebug()<<"返回错误 01";
    }
    this->setRecData(Updata);
    emit sig_UpdataUI();
}

void Thread::slots_wirteModbus(int Number, QVariant data)
{
    QByteArray SendByte;
    int Ret=0;
    switch (Number)
    {
    case 0:
        master_1.data()[0].m_float=data.toFloat();
        SendByte=master_1.WriteDataToByte_10(0,Ret);
        break;
    case 1:
        master_1.data()[1].m_int32=data.toInt();
        SendByte=master_1.WriteDataToByte_10(1,Ret);
        break;
    case 2:
        master_2.data()[0].m_bool=data.toBool();
        SendByte=master_2.WriteDataToByte_05(0,Ret);
        break;
    default:
        break;
    }

    if(!m_SerialPort->isOpen()){
        qDebug()<<"串口未打开";
        return;
    }
    m_SerialPort->write(SendByte);
    m_SerialPort->waitForBytesWritten(30);
    QThread::msleep(100);
    QByteArray RecData_01 = m_SerialPort->readAll();
    while (m_SerialPort->waitForReadyRead(10))
        RecData_01 += m_SerialPort->readAll();

    //返回数据没做判断处理，实际上应该做
}



